package com.company.project.rocketmq.processor;

import org.apache.rocketmq.common.message.MessageExt;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.company.project.constant.TradeEnums;
import com.company.project.model.TradeOrder;
import com.company.project.protocol.mq.CancelOrderMQ;
import com.company.project.rocketmq.IMessageProcessor;
import com.company.project.service.TradeOrderService;

/**
 * 订单模块的 处理 取消订单的 处理逻辑 一般指定 一个 groupName : order_ace_trade_cancel_group
 */
@Component
public class OrderCancelOrderProcessor implements IMessageProcessor {
	public static final Logger LOGGER = LoggerFactory.getLogger(OrderCancelOrderProcessor.class);
	@Autowired
	private TradeOrderService tradeOrderService;

	public boolean handleMessage(MessageExt messageExt) {
		try {
			String body = new String(messageExt.getBody(), "UTF-8");
			String msgId = messageExt.getMsgId();
			String tags = messageExt.getTags();
			String keys = messageExt.getKeys();
			LOGGER.info("OrderCancelOrderProcessor receive message:" + messageExt);
			CancelOrderMQ cancelOrderMQ = JSON.parseObject(body, CancelOrderMQ.class);
			// API 处理：
			// ChangeOrderStatusReq changeOrderStatusReq=new
			// ChangeOrderStatusReq();
			// changeOrderStatusReq.setOrderId(cancelOrderMQ.getOrderId());
			// changeOrderStatusReq.setOrderStatus(TradeEnums.OrderStatusEnum.CANCEL.getStatusCode());
			// orderService.changeOrderStatus(changeOrderStatusReq);

			// 内部直接处理:
			TradeOrder record = new TradeOrder();
			record.setOrderId(cancelOrderMQ.getOrderId());
			record.setOrderStatus(TradeEnums.OrderStatusEnum.CANCEL.getStatusCode());
			tradeOrderService.update(record);
			return true;
		} catch (Exception e) {
			return false;
		}

	}

}
